package com.bc.ui.setting;

import java.util.ArrayList;
import java.util.List;


import com.bc.R;
import com.bc.db.entity.BtcNews;
import com.bc.ui.BaseActivity;
import com.bc.ui.BtcService;
import com.bc.ui.MainActivity;
import com.bc.utils.Constants;
import com.bc.utils.DBUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.GridView;
import android.widget.ImageView;

public class BitcoinSettingActivity extends BaseActivity{
	
	private GridView mGridView;
	ArrayList<BtcNews> availableBtcNews;
	ArrayList<BtcNews> lastBtcNews = new ArrayList<BtcNews>();
	private ActionBar actionBar;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		
		setContentView(R.layout.activity_bitcoin_setting);
		
		availableBtcNews = DBUtils.getAllBtcNews(this);
		
		for (BtcNews bc : availableBtcNews) {
			lastBtcNews.add(bc.clone());
		}
		
		BtcItemAdapter mAdapter = new BtcItemAdapter(this, availableBtcNews);
		mGridView = (GridView)findViewById(R.id.grid_view);
		mGridView.setAdapter(mAdapter);
		mGridView.setOnItemClickListener(clickListener);
		
		actionBar = getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setDisplayShowHomeEnabled(true);
		actionBar.setTitle(getString(R.string.setting_actionbar));
	}
	
	OnItemClickListener clickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View view, int position,
				long arg3) {
			CheckBox checkBox = (CheckBox)view.findViewById(R.id.checkbox);
			boolean state = checkBox.isChecked();
			checkBox.setChecked(!state);
		}
	};
	
	public boolean onCreateOptionsMenu(android.view.Menu menu) {
		getMenuInflater().inflate(R.menu.activity_setting, menu);
		return true;
	};
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_ok:
			saveBtcSelectedState();
			checkAndSendBroadcast();
			
			finish();
			overridePendingTransition(R.anim.push_in_left, R.anim.push_out_right);
			break;
		case android.R.id.home:
			finish();
			overridePendingTransition(R.anim.push_in_left, R.anim.push_out_right);
			
			break;
		default:
			break;
		}
		return true;
	}
	
	public void checkAndSendBroadcast() {
		for (int i = 0; i< availableBtcNews.size(); i++) {
			BtcNews lastBc = lastBtcNews.get(i);
			BtcNews availBc = availableBtcNews.get(i);
			if (lastBc.enable != availBc.enable) {
				Intent intent = new Intent(BtcService.ACTION_NETWORK_BTCNEWS);
				LocalBroadcastManager.getInstance(BitcoinSettingActivity.this).sendBroadcast(intent);
				break;
			}
		}
	}
	
	public synchronized void saveBtcSelectedState() {
		DBUtils.updateBtcEnables(this, availableBtcNews);
	}
	
	@Override
	protected void onDestroy() {
		availableBtcNews = null;
		mGridView = null;
		super.onDestroy();
	}
	
	class BtcItemAdapter extends ArrayAdapter<BtcNews> {

		public BtcItemAdapter(Context context, List<BtcNews> objects) {
			super(context, R.layout.adapter_setting_layout, objects);
		}
		
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(R.layout.adapter_setting_layout, parent, false);
			}
			BtcNews btcNews = getItem(position);
			ImageView imageView = (ImageView)convertView.findViewById(R.id.image);
			CheckBox cb = (CheckBox)convertView.findViewById(R.id.checkbox);
			cb.setChecked(btcNews.enable == 1);
			TextView title = (TextView)convertView.findViewById(R.id.title);
			title.setText(btcNews.btcName);
			
			int id = getResources().getIdentifier(btcNews.btcIcon.trim(), "drawable", getPackageName());
			ImageLoader.getInstance().displayImage("drawable://"+ id, imageView, MainActivity.options);
			cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//					Toast.makeText(getContext(), "state:"+isChecked, Toast.LENGTH_SHORT).show();
					availableBtcNews.get(position).enable = isChecked ? 1 : 0;
				}
			});
			return convertView;
		}
	}
}
